package com.yindian.cos.easy.security.authentication.login.social.core;

import com.yindian.cos.easy.security.authentication.AbstractAuthentication;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 第三方登录的拦截器抽象
 *
 * @author zhangchuanhao
 * @create 2018/11/19
 */
@Log4j2
public abstract class AbstractSocialAuthentication extends AbstractAuthentication {
    /**
     * 该方法会自动跳转到对应的授权页面
     *
     * @param response
     */
    public abstract void redirectAuthorization(HttpServletResponse response) throws IOException;

    /**
     * 第三方登录通用support
     *
     * @param request
     * @param method
     * @param loginUrl
     * @return
     */
    public boolean support(HttpServletRequest request, String method, String loginUrl) {
        String requestUrl = request.getRequestURI();
        String requestMethod = request.getMethod();
        // 验证地址是否匹配
        if (loginUrl.equals(requestUrl)) {
            // 验证方法是否匹配
            if (StringUtils.equalsIgnoreCase(method, requestMethod)) {
                log.debug("执行拦截过滤器{}", this.getClass().getSimpleName());
                return true;
            }
        }
        return false;
    }
}
